<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片区域 -->
    <el-card class="goodsListCard">
      <el-row>
        <el-col :span="8">
          <!-- 搜索输入框 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>

        <el-col :span="4">
          <!-- 添加商品按钮 -->
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" stripe style="width: 100%" size="mini" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70"></el-table-column>
        <el-table-column label="创建时间" width="140">
          <template slot-scope="scope">{{scope.row.upd_time | dateFormat}}</template>
        </el-table-column>
        <!-- 操作按钮区域 -->
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoodById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[8, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      /* 获取商品信息的参数信息 */
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      /* 储存商品信息 */
      goodsList: [],
      /* 商品总条数 */
      total: 0
    }
  },
  methods: {
    /* 发送请求， 获取商品信息 */
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取商品信息失败')
        return
      }
      console.log(res)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    /* 监听 当前页面条数 发生改变 事件 */
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    /* 监听 当前页数 发生改变 事件 */
    handleCurrentChange(newcur) {
      this.queryInfo.pagenum = newcur
      this.getGoodsList()
    },
    /* 点击删除按钮， 发送请求，删除商品 */
    delGoodById(id) {
      this.$confirm('此操作将永久删除该商品', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('goods/' + id)
          if (res.meta.status !== 200) {
            this.$message.error('删除商品失败')
            return
          }
          this.$message.success('删除商品成功')
          this.getGoodsList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    /* 跳转到添加商品页面 */
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="scss" scoped>
.goodsListCard {
  .el-col {
    margin-right: 20px;
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
